package com.hjh.thymeleafdemo.web;

import com.hjh.thymeleafdemo.entity.User;
import com.hjh.thymeleafdemo.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private IUserService userService;
    @RequestMapping(value = "/dologin")
    public String dologin(String usrName, String usrPassword, HttpServletRequest request){
        User user = userService.login(usrName,usrPassword);
        if (user != null){
            request.getSession().setAttribute("loginUser",user);
            return "main";
        }else {
            request.setAttribute("message","登录失败，用户名或密码错误！");
            return "login";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "login";
    }

    @GetMapping(value = "/user/list")
    public String findUsers(String usrName, Long roleId, Model model){
        List<User> list = userService.findAllUsers();
        model.addAttribute("users",list);
        return "/user/list";
    }

}
